#include <stdio.h>
#include <strings.h>
#include <netinet/in.h>

int main(int argc, char **argv)
{
	int x;
	struct sockaddr_in adr_inet;
	int len_inet;
	unsigned msb;
	char class;
	char *netmask;

	static struct {
		unsigned char ip[4];
	} address [] = {
			{ { 44, 135, 86, 12 } },
			{ { 127, 0, 0, 1 } },
			{ { 172, 16, 23, 95 } },
			{ { 241, 68, 9, 1 } } 
	};

	for(x = 0; x<4; x++) {
		bzero((char *) &adr_inet.sin_addr.s_addr, sizeof(adr_inet));
		adr_inet.sin_family = AF_INET;
		adr_inet.sin_port = htons(9000);
		bcopy(address[x].ip, &adr_inet.sin_addr.s_addr, 4);
		len_inet = sizeof(adr_inet);

		msb = *(unsigned char *) &adr_inet.sin_addr.s_addr;

		if((msb & 0x80) == 0x00) {
			class = 'A';
			netmask = "255.0.0.0";
		} else if((msb & 0xC0) == 0x80) {
			class = 'B';
			netmask = "255.255.0.0";
		} else if((msb & 0xE0) == 0xC0) {
			class = 'C';
			netmask = "255.255.255.0";
		} else if((msb & 0xF0) == 0xE0) {
			class = 'D';
			netmask = "255.255.255.255";
		} else {
			class = 'E';
			netmask = "255.255.255.255";
		}

		printf("Address %u.%u.%u.%u is class %c netmask %s\n", address[x].ip[0], address[x].ip[1], address[x].ip[2], address[x].ip[3], class, netmask);
	}

	return 0;
}
